// components/system/see_a_doctor_for_fish/see_a _doctor_for_fish.js
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        activeKey: 0,
        reply_0_num: '',
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onChange(event) {
            console.log("onchange")
            this.setData({
                activeKey: event.detail
            })
            console.log(this.data.activeKey)
        },

        onReachBottom() {

            if (this.data.activeKey == 0) {
                console.log("给鱼看病上拉触底0")
                let reply_0 = this.selectComponent("#reply_0 ")
                reply_0.onReachBottom()
            } else {
                console.log("给鱼看病上拉触底1")
                let reply_1 = this.selectComponent("#reply_1")
                reply_1.onReachBottom()
            }


        }

    },
    lifetimes: {
        attached() {

            let id = wx.getStorageSync('userId')

            wx.request({
                url: `http://120.77.156.205:8808/wx/disease/getDiseaseVoAmount/${id}/0`,
                method: 'GET',
                success: (res) => {

                    if (res.data.data.count != 0) {
                        this.setData({
                            reply_0_num: String(res.data.data.count)
                        })
                        wx.setTabBarBadge({
                            index: 2,
                            text: String(res.data.data.count),
                        })
                    } else {
                        this.setData({
                            reply_0_num: ''
                        })
                        //如果数量为0，隐藏徽标提示
                       wx.removeTabBarBadge({
                         index: 2,
                       })
                    }
                }
            })

            // 定时查询未回复的数量，并显示徽标
            setInterval(() => {

                const pages = getCurrentPages();

                const route = pages[pages.length - 1].route;
                // console.log(route)
                // 如果当前页面路径是tarbar页面，则每隔3s更新专家系统的徽标
                if (route == "pages/index/index" || route == "pages/installation/installation" || route == "pages/systems/systems" || route == "pages/other/other") {

                    wx.request({
                        url: `http://120.77.156.205:8808/wx/disease/getDiseaseVoAmount/${id}/0`,
                        method: 'GET',
                        success: (res) => {

                            if (res.data.data.count != 0) {
                                if (String(res.data.data.count) != this.data.count) {
                                    this.setData({
                                        reply_0_num: String(res.data.data.count)
                                    })
                                    wx.setTabBarBadge({
                                        index: 2,
                                        text: String(res.data.data.count),
                                    })

                                }

                            } else {
                                this.setData({
                                    reply_0_num: ''
                                })
                                //如果数量为0，隐藏徽标提示
                                wx.removeTabBarBadge({
                                  index: 2,
                                })
                            }
                        }
                    })
                }
            }, 2000)
        }
    }

})
